﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CMS.BLL;
using CMS.BLL.Entity;

public partial class AdminX_Tour_SaleOffAdd : System.Web.UI.UserControl
{
    protected string sTodate = "";
    protected string sFromdate = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        Alert1.HideMessage();
        if (!IsPostBack)
        {
            ValidateControl(); 
            SetLabelLanguage();
            BindDataToLanguageDropDown();
            BindDataWhenEdit();
        }
    }

    #region BindDataToDDL
    private void SetLabelLanguage()
    {
        int nDefaultLanguage = Support.GetDefaultLanguage();
        btnBack.Text = MultipleLanguage.BTN_BACK[nDefaultLanguage - 1];
        btnSave.Text = MultipleLanguage.BTN_SAVE[nDefaultLanguage - 1];
        
        lblTitle.Text = MultipleLanguage.SALEOFF[nDefaultLanguage - 1];
        lblFromDate.Text = MultipleLanguage.LBL_SALEOFF_FROMDATE[nDefaultLanguage - 1];
        lblToDate.Text = MultipleLanguage.LBL_SALEOFF_TODATE[nDefaultLanguage - 1];
        lblContent.Text = MultipleLanguage.LBL_CONTENT[nDefaultLanguage - 1];
        lblLanguage.Text = MultipleLanguage.LBL_LANGUAGE[nDefaultLanguage - 1];
    }
    private void BindDataToLanguageDropDown()
    {
        Languages objLanguage = new Languages();
        DataTable dtbLanguage = new DataTable();
        dtbLanguage = objLanguage.Search();
        ddlLanguage.DataSource = dtbLanguage;
        ddlLanguage.DataValueField = "LanguageID";
        ddlLanguage.DataTextField = "Description";
        ddlLanguage.DataBind();
    }
    protected void ddlLanguage_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindDataWhenEdit();
    }
    #endregion

    #region Relative Functions
    private void ValidateControl()
    {
        //rfvTitle.ErrorMessage = MessagesValidate.SafeOff_EMPTY;
    }
    private void BindDataWhenEdit()
    {
        int nCurrentLanguage = ConvertData.ConvertToInt(ddlLanguage.SelectedValue);
        string sAction = ConvertData.ConvertToString(Request.QueryString[Config.ACTION]);
        try
        {
            if (sAction == Config.ACTION_EDIT)
            {
                int nID = ConvertData.ConvertToInt(Request.QueryString[Config.ACTION_ID]);
                if (nID > 0)
                {
                    SaleOff objSaleOff = new SaleOff();
                    objSaleOff.LoadById(nID);
                    switch (nCurrentLanguage)
                    {
                        case 1:
                            txtTitle.Text = ConvertData.ConvertToString(objSaleOff.Data.Title1);
                            FTBContent.IntialText(ConvertData.ConvertToString(objSaleOff.Data.Description1));
                            break;
                        case 2:
                            txtTitle.Text = ConvertData.ConvertToString(objSaleOff.Data.Title2);
                            FTBContent.IntialText(ConvertData.ConvertToString(objSaleOff.Data.Description2));
                            break;
                        case 3:
                            txtTitle.Text = ConvertData.ConvertToString(objSaleOff.Data.Title3);
                            FTBContent.IntialText(ConvertData.ConvertToString(objSaleOff.Data.Description3));
                            break;
                    }

                    if (objSaleOff.Data.ToDate > 0)
                        sTodate = ConvertData.ConvertIntToDatetimeString(objSaleOff.Data.ToDate);
                    else
                        sTodate = ConvertData.ConvertIntToDatetimeString(ConvertData.ConvertDatetimeToInt(DateTime.Today));
                    if (objSaleOff.Data.FromDate > 0)
                        sFromdate = ConvertData.ConvertIntToDatetimeString(objSaleOff.Data.FromDate);
                    else
                        sFromdate = ConvertData.ConvertIntToDatetimeString(ConvertData.ConvertDatetimeToInt(DateTime.Today));
                }
            }
            else
            {
                sTodate = ConvertData.ConvertIntToDatetimeString(ConvertData.ConvertDatetimeToInt(DateTime.Today));
                sFromdate = ConvertData.ConvertIntToDatetimeString(ConvertData.ConvertDatetimeToInt(DateTime.Today));
            }
        }
        catch
        {
            string sLink = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_TOURSALEOFF;
            Response.Redirect(sLink);
        }
    }
    private eSaleOff SetData()
    {
        SaleOff objSaleOff = new SaleOff();
        eSaleOff eSaleOff = this.SaleOffEntity();
        int nCurrentLanguage = ConvertData.ConvertToInt(ddlLanguage.SelectedValue);
        
        string sDate1 = Request.Form["date1"].ToString();
        objSaleOff.Data.FromDate = ConvertData.ConvertDateStringToInt(sDate1);
        string sDate2 = Request.Form["date2"].ToString();
        objSaleOff.Data.ToDate = ConvertData.ConvertDateStringToInt(sDate2);
        int dtToDay = ConvertData.ConvertDatetimeToInt(DateTime.Today);
        if (dtToDay > objSaleOff.Data.FromDate)
        {
            objSaleOff.Data.Status = 0;
        }
        else
        {
            objSaleOff.Data.Status = 1;
        }

        switch (nCurrentLanguage)
        {
            case 1:
                objSaleOff.Data.Description1 = ConvertData.ConvertToString(FTBContent.RenderText());
                objSaleOff.Data.Title1 = ConvertData.ConvertToString(txtTitle.Text);

                objSaleOff.Data.Title2 = ConvertData.ConvertToString(eSaleOff.Title2);
                objSaleOff.Data.Title3 = ConvertData.ConvertToString(eSaleOff.Title3);
                objSaleOff.Data.Description2 = ConvertData.ConvertToString(eSaleOff.Description2);
                objSaleOff.Data.Description3 = ConvertData.ConvertToString(eSaleOff.Description3);
                break;
            case 2:
                objSaleOff.Data.Description2 = ConvertData.ConvertToString(FTBContent.RenderText());
                objSaleOff.Data.Title2 = ConvertData.ConvertToString(txtTitle.Text);

                objSaleOff.Data.Title1 = ConvertData.ConvertToString(eSaleOff.Title1);
                objSaleOff.Data.Title3 = ConvertData.ConvertToString(eSaleOff.Title3);
                objSaleOff.Data.Description1 = ConvertData.ConvertToString(eSaleOff.Description1);
                objSaleOff.Data.Description3 = ConvertData.ConvertToString(eSaleOff.Description3);
                break;
            case 3:
                objSaleOff.Data.Title3 = ConvertData.ConvertToString(txtTitle.Text);
                objSaleOff.Data.Description3 = ConvertData.ConvertToString(FTBContent.RenderText());

                objSaleOff.Data.Title1 = ConvertData.ConvertToString(eSaleOff.Title1);
                objSaleOff.Data.Title2 = ConvertData.ConvertToString(eSaleOff.Title2);
                objSaleOff.Data.Description1 = ConvertData.ConvertToString(eSaleOff.Description1);
                objSaleOff.Data.Description2 = ConvertData.ConvertToString(eSaleOff.Description2);
                break;
        }
        
        return objSaleOff.Data;
    }
    private eSaleOff SaleOffEntity()
    {
        eSaleOff entitySaleOff = new eSaleOff();
        int nItemID = ConvertData.ConvertToInt(Request.QueryString[Config.ACTION_ID]);
        string sQueery = ConvertData.ConvertToString(Request.QueryString[Config.ACTION]);
        if (nItemID > 0 && sQueery == Config.ACTION_EDIT)
        {
            SaleOff objSaleOff = new SaleOff();
            objSaleOff.LoadById(nItemID);
            entitySaleOff = objSaleOff.Data;
        }
        return entitySaleOff;
    }
    #endregion

    #region Click Events
    protected void btnSave_Click(object sender, EventArgs e)
    {
        int nDefaultLanguage = Support.GetDefaultLanguage() - 1;
        SaleOff objSaleOff = new SaleOff();

        objSaleOff.Data = this.SetData();
        int nResult = 0;
        string sLink = "";
        string sAction = Request.QueryString[Config.ACTION];
        switch (sAction)
        {
            case Config.ACTION_ADD:
                nResult = objSaleOff.Insert();
                if (nResult > 0)
                {
                    sLink = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_TOURSALEOFF + "&" + Config.MESS_ID + "=1";
                    Response.Redirect(sLink);
                }
                else Alert1.ShowMessage(2, MessagesAlert.INSERT_UNSUCCESSFUL[nDefaultLanguage]);
                break;
            case Config.ACTION_EDIT:
                int nID = ConvertData.ConvertToInt(Request.QueryString[Config.ACTION_ID]);
                nResult = objSaleOff.Update(nID);
                if (nResult > 0)
                {
                    sLink = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_TOURSALEOFF + "&" + Config.MESS_ID + "=2";
                    Response.Redirect(sLink);
                }
                else
                    Alert1.ShowMessage(2, MessagesAlert.UPDATE_UNSUCCESSFUL[nDefaultLanguage]);
                break;
        }
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect(Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_TOURSALEOFF);

    }
    #endregion
}